<?php
// 1. 退出操作后，清除自动登录令牌

$isLogout = false;

if (isset($_SESSION['logout']) && $_SESSION['logout'] === true ) {
    $isLogout = true;
    setcookie('remember-token', '', time() - 3600);
    unset($_SESSION['logout']);
}

// 2. 判断当前用户是否登录

$isSignin = isset($_SESSION['user']) && !empty($_SESSION['user']) ? true : false;

$user = $isSignin ? $_SESSION['user'] : null;

// 3. 自动登录

if (isset($_COOKIE['remember-token']) && !empty($_COOKIE['remember-token']) && !$isLogout) {

    if (!$isSignin) {
        $rememberToken = $_COOKIE['remember-token'];
        $sql = 'select id,user_name,password from users where remember_token=?';
        $data = query($sql, [$rememberToken]);
        $user = $data[0];
        $_SESSION['user'] = $user;
        $isSignin = true;
    }
}

// 4. 取出一次性的错误包信息

$hasError = isset($_SESSION['has_error']) ? true : false;

$errorBag = isset($_SESSION['error_bag']) ? $_SESSION['error_bag'] : ['error' => ''];
// 5. 清除session中的一次性错误信息

if (isset($_SESSION['has_error']) && isset($_SESSION['error_bag'])) {

   unset($_SESSION['has_error']);
   unset($_SESSION['error_bag']);
}

// 6. 判断用户登录时是否勾选了记住我

if (isset($_SESSION['remember-token']) && !empty($_SESSION['remember-token'])) {

    $rememberToken = $_SESSION['remember-token'];
    unset($_SESSION['remember-token']);
    // 把remember-token写入cookie

    setcookie('remember-token', $rememberToken, time() + 3600);

}

// 查询留言信息

$sql = 'select id, user_name,user_avatar, content, created_at, like_num from messages order by created_at desc';

$messages = query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>留言板</title>
    <link rel="stylesheet" href="<?php echo assets('css/style.css') ?>">
    <link rel="stylesheet" href="<?php echo assets('css/comment.css') ?>">
</head>
<body>
<div class="commentAll">
    <!--用户信息区域-->
    <div class="comment-show-user">
        <div class="comment-show-user-con clearfix">
            <div class="comment-show-con-img pull-left"><img src="<?php echo assets($user['avatar']) ?>" alt=""></div>
            <div class="comment-show-con-list pull-left clearfix" style="width: 200px;display: inline-block" >
                <div class="pl-text clearfix"  >
                   <?php if ($isSignin): ?>
                    <a href="#" class="comment-username"><?=$user['user_name'] ?></a>
                   <?php endif; ?> 
                </div>
            </div>
            <?php if($isSignin): ?>
              <form method="post" action="<?php echo url('logout_action') ?>">
                 <input class="outBtn" style="outline: none;border: none;" type="submit" value="退出" />
              </form>
            <?php else: ?>  
              <a href="<?php echo url('signin') ?>" class="inBtn">登录</a> 
            <?php endif; ?>  
        </div>
    </div>
    <!--评论区域 begin-->
    <div class="reviewArea clearfix">
        <form action="<?php echo url('post_message') ?>" method="post">
            <textarea name="content" style="max-height: 100px;max-width: 470px;" class="content comment-input" placeholder="请输入留言内容&hellip;"></textarea>
            <div style="font-size:12px;text-align:center;color: #c00;">
                <?php if($hasError) echo $errorBag['error']; ?>
            </div>
            <button type="submit" class="plBtn">留言</button>
        </form>
    </div>
    <!--评论区域 end-->
 <?php foreach ($messages as $msg): ?>
    <div class="comment-show">
        <div class="comment-show-con clearfix">
            <div class="comment-show-con-img pull-left">
            <?php $avatar = empty($msg['user_avatar']) ? 'images/header-img-comment_03.png' :  $msg['user_avatar']?>
               <img src="<?php echo assets($avatar) ?>" alt=""></div>
            <div class="comment-show-con-list pull-left clearfix">
                <div class="pl-text clearfix">
                    <a href="#" class="comment-size-name"><?=$msg['user_name'] ?> : </a>
                    <span class="my-pl-con">&nbsp;<?=$msg['content']?></span>
                </div>
                <div class="date-dz">
                    <span class="date-dz-left pull-left comment-time"><?=$msg['created_at']?></span>
                    <div class="date-dz-right pull-right comment-pl-block">
                       <?php if($user['user_name'] == $msg['user_name']): ?>
                        <a href="<?php echo url('delete_message') ?>?id=<?=$msg['id']?>" class="removeBlock">删除</a>
                       <?php endif; ?>  
                        <a href="javascript:;" class="date-dz-pl pl-hf hf-con-block pull-left">回复</a>
                        <span class="pull-left date-dz-line">|</span>
                        <a href="javascript:;" class="date-dz-z pull-left"><i class="date-dz-z-click-red"></i>赞 (<i class="z-num"><?=$msg['like_num']?></i>)</a>
                    </div>
                </div>
                <div class="hf-list-con"></div>
            </div>
        </div>
    </div>
  <?php endforeach; ?>
</body>
</html>